﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.Document
{
    /// <summary>
    /// ApproveDocument 的摘要说明。
    /// </summary>
    public partial class ApproveDocumentPage : Page
    {
        public static int DocID;

        protected override void OnInit(EventArgs e)
        {
            btnApprove.Click += btnApprove_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DocID = Int32.Parse(Request.QueryString["DocID"]);
                ShowBodyDetail();
            }

            base.OnLoad(e);
        }

        private void btnApprove_Click(object sender, EventArgs e)
        {
            var mySQL = new Database();
            string UserName = SiteContext.Current.UserName;

            SqlParameter[] parameters = {
                Database.MakeInParam("@DocIDs", SqlDbType.NVarChar, 300, DocID),
                Database.MakeInParam("@Approver", SqlDbType.NVarChar, 300, UserName)
            };

            if (mySQL.RunProc("dbo.sproc_ApproveDocument", parameters) > 0)
            {
                Response.Write("<script language='javascript'>window.alert('审批文件成功!');self.location='../Desktop.aspx';</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>window.alert('审批文件成功!');self.location='../Desktop.aspx';</script>");
            }
        }

        #region 使用DataReader显示文档内容

        protected void ShowBodyDetail()
        {
            var doc = new DocumentClass();
            using (SqlDataReader dataReader = doc.GetDocDetail(DocID, SiteContext.Current.UserName))
            {
                while (dataReader.Read())
                {
                    lblDocTitle.Text = dataReader["DocTitle"].ToString();
                    lblDocApprover.Text = Staff.GetRealNameByUsername(dataReader["DocApprover"].ToString());
                    lblDocApproveDate.Text = dataReader["DocApproveDate"].ToString() == "1900-1-1 00:00:00" ? "" : dataReader["DocApproveDate"].ToString();
                    lblAddedBy.Text = Staff.GetRealNameByUsername(dataReader["DocAddedby"].ToString());
                    lblAddedDate.Text = dataReader["DocAddedDate"].ToString();
                    lblDocViewedTimes.Text = dataReader["DocViewedTimes"].ToString();
                    lblClassName.Text = dataReader["ClassName"].ToString();
                    lblDocContent.Text = dataReader["DocContent"].ToString();
                    if (dataReader["FileCatlog"].ToString() != "")
                    {
                        if (dataReader["FileCatlog"].ToString() == "Mail")
                        {
                            lblFileVisualPath.Text += "&nbsp;<a href='Download.aspx?destFileName=\\" + dataReader["FileVisualPath"] + "'>" + dataReader["FileName"] + "</a><br>";
                        }
                            //this.lblFileVisualPath.Text += "<a href='../"+dataReader[21].ToString()+"'>"+dataReader[18].ToString()+"</a><br>&nbsp;&nbsp;";
                        else
                        {
                            lblFileVisualPath.Text += "<a href='Download.aspx?destFileName=\\Document\\" + dataReader["FileVisualPath"] + "'>" + dataReader["FileName"] + "</a><br>&nbsp;&nbsp;";
                        }
                    }
                }

                dataReader.Close();
            }
        }

        #endregion
    }
}